import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**属性枚举*/
export const enum E_LegionLog {
	/** 日志时间*/
	LOG_TIME = 0,
	/** 日志内容*/
	CONTENT = 1,
	/** 操作者ID*/
	OPERATOR_ID = 2,
	/**枚举数量*/
	EnumCount = 3
}


/***/
export class LegionLog {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_LEGION_LOG_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_LEGION_LOG_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: LegionLog[] = [];

	/**创建入口*/
	public static create(json?: string): LegionLog {
		const c = this.POOL.pop() || new LegionLog();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/** 日志时间 (long)*/
	protected _logTime: number;
	/** 日志内容*/
	protected _content: string;
	/** 操作者ID (uint32)*/
	protected _operatorId: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (LegionLog.POOL.indexOf(this) === -1) {
			LegionLog.POOL.push(this);
		}
	}

	/** 日志时间*/
	public get logTime() {
		return this._logTime;
	}


	/** 日志时间*/
	public set logTime(v: number) {
		this._logTime = v;
	}


	/** 日志内容*/
	public get content() {
		return this._content;
	}


	/** 日志内容*/
	public set content(v: string) {
		this._content = v;
	}


	/** 操作者ID*/
	public get operatorId() {
		return this._operatorId;
	}


	/** 操作者ID*/
	public set operatorId(v: number) {
		this._operatorId = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_LegionLog): any {
		switch (k) {
			case E_LegionLog.LOG_TIME: return this._logTime;
			case E_LegionLog.CONTENT: return this._content;
			case E_LegionLog.OPERATOR_ID: return this._operatorId;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_LegionLog, v: any): void {
		switch (k) {
			case E_LegionLog.LOG_TIME: this._logTime = v; break;
			case E_LegionLog.CONTENT: this._content = v; break;
			case E_LegionLog.OPERATOR_ID: this._operatorId = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		// 日志时间
		this._logTime = 0;
		// 日志内容
		this._content = '';
		// 操作者ID
		this._operatorId = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._logTime) {
			json.logTime = this._logTime;
		}
		if (this._content) {
			json.content = this._content;
		}
		if (this._operatorId) {
			json.operatorId = this._operatorId;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.logTime) {
			this._logTime = json.logTime;
		}
		if (json.content) {
			this._content = json.content;
		}
		if (json.operatorId) {
			this._operatorId = json.operatorId;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeLongNumber(this._logTime);
		byte.writeString(this._content);
		byte.writeUint32(this._operatorId);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._logTime = byte.readLongNumber();
		this._content = byte.readString();
		this._operatorId = byte.readUint32();
		return this;
	}


	/**克隆*/
	public clone(): LegionLog {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = LegionLog.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = LegionLog.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			// 日志时间
			if (idx === E_LegionLog.LOG_TIME) {
				const oldv = this._logTime;
				this._logTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._logTime, oldv);
				i += 2;
				continue;
			}
			// 日志内容
			if (idx === E_LegionLog.CONTENT) {
				const oldv = this._content;
				this._content = us[i + 1];
				IAsync.dispatch(eventID, idx, this._content, oldv);
				i += 2;
				continue;
			}
			// 操作者ID
			if (idx === E_LegionLog.OPERATOR_ID) {
				const oldv = this._operatorId;
				this._operatorId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._operatorId, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in LegionLog with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: LegionLog): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._logTime !== other._logTime) {
			updates.push(E_LegionLog.LOG_TIME, this._logTime);
		}
		if (this._content !== other._content) {
			updates.push(E_LegionLog.CONTENT, this._content);
		}
		if (this._operatorId !== other._operatorId) {
			updates.push(E_LegionLog.OPERATOR_ID, this._operatorId);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(LegionLog.TOTAL_EVENT_ID, this.getV(0));
	}

}
